<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $phone_number
 * @property string $status
 * @property string $salt
 * @property integer $total_points
 * @property string $current_batch
 * @property string $current_url
 * @property string $org_id
 * @property string $activation_code 
 * @property string $movie_mode
 * @property string $current_grade
 * @property string $created
 * @property string $modified
 */
class Users extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Users the static model class
	 */ 
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password', 'required'),  
			array('username', 'email'),
			array('username', 'unique','on'=>'register'),
			array('total_points', 'numerical', 'integerOnly'=>true),
			array('username', 'length', 'max'=>300),
			array('password', 'length', 'max'=>500),
			array('status', 'length', 'max'=>8),
			array('current_batch', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, password, status, total_points, current_batch, created, modified', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Username',
			'password' => 'Password',
			'status' => 'Status',
			'total_points' => 'Total Points',
			'current_batch' => 'Current Batch',
			'created' => 'Created',
			'modified' => 'Modified',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('total_points',$this->total_points);
		$criteria->compare('current_batch',$this->current_batch,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	public function validatePassword($password)
	{
		return md5($password.$this->salt)== $this->password;
	}
		//get  model object of specific type
	public function getModel($type, $arrParams) 
	{
		switch($type)
		{		
			case "USER-ID":
				$user=Users::model()->findByPk((int)$arrParams['userid']);
				return $user;
			break;
			case "USERNAME":
				$user=Users::model()->find(
					'username=?',array($arrParams['username'])
					);
				return $user;
			break;
			case "ACTIVATION-CODE":
				$user=Users::model()->find(
					'activation_code=?',array($arrParams['acode'])
					);
				return $user;
			break;
		}
	}
}